// pages/recharge/recharge.js
var basePage = require('../../../common/BasePage');
var baseList = require('../../../common/BaseList');
Page({
    behaviors: [basePage, baseList],
    /**
     * 页面的初始数据
     */
    data: {
        setMeal: [],
        selectIndex: 0,
        disabled: false,
        wh_id:1,
        // pay: "allin",
        // payType: 1,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            wh_id:options.wh_id
        })
        this.getMeal()
        this.getPayType(1)
    },
    getMeal() {
        this.request({
            url: this.data.host + "/home/user/package",

        }, res => {
            this.setData({
                setMeal: res.data
            })
        })
    },
    selectMeal(e) {
        this.setData({
            selectIndex: e.currentTarget.dataset.index
        })
    },
    submit_click() {
        if (this.data.disabled) return;
        var info = this.data.setMeal[this.data.selectIndex]
        var param = {
            pack_id: info.pack_id,
            // code:"wxpay"
            code: this.data.pay,
            wh_id:this.data.wh_id
        }
        this.setData({
            disabled: true
        })
        var that = this
        this.request({
            url: this.data.host + "/home/payment/coin",
            data: param,
            method: "POST"

        }, res => {
            if (this.data.pay == "allin") {
                //通联支付
                var payInfo = res.data.payInfo
                if (payInfo) {
                    payInfo = JSON.parse(payInfo)
                    wx.requestPayment({
                        ...payInfo,
                        success(res) {
                            wx.showToast({
                                title: '支付成功',
                            })
                        },
                        fail(err) {
                            wx.showToast({
                                title: '支付失败',
                                icon: 'none'
                            })
                            console.error('pay fail', err)
                        },
                        complete() {
                            that.setData({
                                disabled: false
                            })
    
    
                        }
                    })
                }else if(res.data.extraData){//跳转收银台小程序
                    var extraData=res.data.extraData

                    this.alinePay(extraData,this.pay_ok)

                }else{
                    wx.showToast({
                      title: '支付失败',
                      icon:"none"
                    })
                    this.setData({
                        disabled: false
                    })
                }
                
            } else if (this.data.pay == "wxpay") {
                wx.requestPayment({
                    ...res.data.pay_data,
                    success(res) {
                        wx.showToast({
                            title: '支付成功',
                        })

                    },
                    fail(err) {
                        wx.showToast({
                            title: '支付失败',
                            icon: 'none'
                        })
                    },
                    complete() {
                        that.setData({
                            disabled: false
                        })
                    }
                })
            }


        }, null, () => {
            this.setData({
                disabled: false
            })
        })
    },
    pay_ok(){
        wx.showToast({
            title: '支付成功',
        })
        this.setData({
            disabled: false
        })
    },
    toWallet() {
        wx.redirectTo({
            url: '/pages/profit/wallet/wallet?type=2&wh_id='+this.data.wh_id,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})